﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace Social_Celebrities
{
    public class ImageFromRssText1 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (System.Convert.ToInt32(value.ToString()) == 0)
                return new BitmapImage(new Uri("/Images/plusicons.png", UriKind.RelativeOrAbsolute));
            else
                return new BitmapImage(new Uri("/Images/box.png", UriKind.RelativeOrAbsolute));
        }

        public static List<ImageItem> GetHtmlImageUrlList(string sHtmlText)
        {
            Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            List<ImageItem> imgUrlList = new List<ImageItem>();
            foreach (Match match in matches)
            {
                imgUrlList.Add(new ImageItem("img" + i, match.Groups["imgUrl"].Value));
                i++;
            }
            return imgUrlList;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
   
    public class ImageFromRssText : IValueConverter
    {
       
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;
            List<ImageItem> listUri = GetHtmlImageUrlList(value.ToString());
            if (listUri.Count > 0)
            listUri[0].URL = listUri[0].URL.Replace("amp;", "");
            return listUri;
        }

        public static List<ImageItem> GetHtmlImageUrlList(string sHtmlText)
        {

            Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            List<ImageItem> imgUrlList = new List<ImageItem>();
            foreach (Match match in matches)
            {
                imgUrlList.Add(new ImageItem("img" + i, match.Groups["imgUrl"].Value));
                i++;
            }
            return imgUrlList;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
   
    public class VideoFromRssText : IValueConverter
    {
       public  ObservableCollection<ImageItems>  urllist;
       
       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           urllist = new ObservableCollection<ImageItems>();
           if (value == null) return null;
           MatchCollection matchevideos = Regex.Matches(value.ToString(), @"https?://(?:www.)?facebook.com/(?:video.php\?v=)?(.+)",RegexOptions.IgnoreCase);
           if (matchevideos.Count > 0)
           {
               string videoUrl = string.Empty;
               List<ImageItems> videoUrlList = new List<ImageItems>();
               foreach (Match matchv in matchevideos)
               {
                   string url = matchv.Groups[0].Value;
                   videoUrl = url.Split('"')[0];
               }
               if (videoUrl.Contains("www.facebook.com/video.php"))
               {
                   WebClient client = new WebClient();
                   client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12";
                   client.DownloadStringAsync(new Uri(videoUrl), UriKind.Absolute);
                   client.DownloadStringCompleted += client_DownloadStringCompleted;
               }
               else
               {
                   urllist.Add(new ImageItems("text", "", "", Visibility.Collapsed, Visibility.Collapsed));
               }
           }
           else
           {
               GetHtmlImageUrlList(value.ToString());
               if (urllist.Count > 0)
                   urllist[0].ImageURL1 = urllist[0].ImageURL1.Replace("amp;", "");
               else if (urllist.Count == 0)
               {
                   urllist.Add(new ImageItems("text", "", "", Visibility.Collapsed, Visibility.Collapsed));
               }
           }

           return urllist;

       }
       public void GetHtmlImageUrlList(string sHtmlText)
       {

           Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
           MatchCollection matches = regImg.Matches(sHtmlText);
           int i = 0;
           ObservableCollection<ImageItems> imgUrlList = new ObservableCollection<ImageItems>();
           foreach (Match match in matches)
           {
               urllist.Add(new ImageItems("img" + i, match.Groups["imgUrl"].Value, "", Visibility.Visible, Visibility.Collapsed));
               i++;
           }
       }
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            string html = e.Result.ToString();
            string val = Regex.Split(html, "sd_src")[1];
            string mm = Regex.Split(val, "thumbnail_src")[0];
            string astring = mm.Replace("\\u00253A", ":");
            astring = astring.Replace("\\u00255C\\u00252F", "/");
            astring = astring.Replace("\\u00253F", "?");
            astring = astring.Replace("\\u00253D", "=");
            astring = astring.Replace("\\u002526", "&");
            astring = astring.Replace("\\u002522:\\u002522", "");
            astring = astring.Replace("\\u002522\\u00252C\\u002522", "");
            urllist.Add(new ImageItems("Video", "", astring.Replace("amp;", ""), Visibility.Collapsed, Visibility.Visible));
        }
       
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
  
    public class ImageFromRssText2 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;
            List<ImageItem> listUri = GetHtmlImageUrlList(value.ToString());
            if (listUri.Count > 0)
                listUri[0].URL = listUri[0].URL.Replace("amp;", "");
            if (listUri.Count > 0)
                return listUri[0].URL;
            else
                return null;
        }
        public static List<ImageItem> GetHtmlImageUrlList(string sHtmlText)
        {
           
            Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            List<ImageItem> imgUrlList = new List<ImageItem>();
            foreach (Match match in matches)
            {
                imgUrlList.Add(new ImageItem("img" + i, match.Groups["imgUrl"].Value));
                i++;
            }
            return imgUrlList;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ImageItem
    {
        public ImageItem(string title, string url)
        {
            this.Title = title;
            this.URL = url;
        }

        public string Title { get; set; }
        public string URL { get; set; }
    }
    public class ImageItems
    {
        public ImageItems(string title, string imageurl, string videourl, Visibility imagevisibility, Visibility videovisibility)
        {
            this.Title1 = title;
            this.ImageURL1 = imageurl;
            this.VideoURL1 = videourl;
            this.ImageVisibility = imagevisibility;
            this.VideoVisibility = videovisibility;
        }

        public string Title1 { get; set; }
        public string ImageURL1 { get; set; }
        public string VideoURL1 { get; set; }
        public Visibility ImageVisibility { get; set; }
        public Visibility VideoVisibility { get; set; }
    }
}